//
//  Common.swift
//  YLTextView
//
//  Created by emma on 2023/8/18.
//

import Foundation
import UIKit

//屏幕高度
let kScreenHeight = UIScreen.main.bounds.height
//屏幕宽度
let kScreenWidth = UIScreen.main.bounds.width

//frame
func rect(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
    return CGRect(x: x, y: y, width: width, height: height)
}

//MARK: - 颜色
//生成颜色RGB
func rgba(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat = 1.0) -> UIColor {
    return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
func rgb(_ rgb: CGFloat) -> UIColor {
    return UIColor(red: rgb/255.0, green: rgb/255.0, blue: rgb/255.0, alpha: 1.0)
}
//16进制颜色 可以这样调用：hexColor(0x1D1D26)
func hexColor(hexValue: Int) -> UIColor {
    return UIColor(red: ((CGFloat)((hexValue & 0xFF0000) >> 16)) / 255.0, green: ((CGFloat)((hexValue & 0xFF00) >> 8)) / 255.0, blue: ((CGFloat)(hexValue & 0xFF)) / 255.0, alpha: 1.0)
}

//MARK: - 字体
func regular(_ size: CGFloat) -> UIFont {
    return UIFont(name: "PingFangSC-Regular", size: size) ?? UIFont.systemFont(ofSize: size)
}
func medium(_ size: CGFloat) -> UIFont {
    return UIFont(name: "PingFangSC-Medium", size: size) ?? UIFont.systemFont(ofSize: size)
}
func bold(_ size: CGFloat) -> UIFont {
    return UIFont(name: "PingFangSC-Semibold", size: size) ?? UIFont.systemFont(ofSize: size)
}

//MARK: - 自定义打印
func MyLog<T>(_ message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
    #if DEBUG
        let fileName = (file as NSString).lastPathComponent
        print("[\(fileName)--\(lineNum)行--]:\(message)")
        //print("文件:\(fileName)\n方法:\(funcName)\n行号:\(lineNum)\n打印信息\(message)");
    #endif
}

extension String {
    //MARK: - 获取宽度、高度
    func getWidth(_ font: UIFont, _ height: CGFloat) -> CGFloat {
        let size = self.boundingRect(with: CGSize(width:900, height: height), options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil).size
        return size.width
    }
    func getHeight(_ font: UIFont, _ width: CGFloat) -> CGFloat {
        let size = self.boundingRect(with: CGSize(width:width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil).size
        return size.height
    }
}
